package org.jivesoftware.smackx;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.ConnectionCreationListener;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smack.packet.XMPPError.Condition;
import org.jivesoftware.smackx.entitycaps.EntityCapsManager;
import org.jivesoftware.smackx.entitycaps.EntityCapsManager.NodeVerHash;
import org.jivesoftware.smackx.packet.DataForm;
import org.jivesoftware.smackx.packet.DiscoverInfo;
import org.jivesoftware.smackx.packet.DiscoverInfo.Identity;
import org.jivesoftware.smackx.packet.DiscoverItems;

public class ServiceDiscoveryManager
{
  private static final String DEFAULT_IDENTITY_NAME = "Smack";
  private static final String DEFAULT_IDENTITY_CATEGORY = "client";
  private static final String DEFAULT_IDENTITY_TYPE = "pc";
  private static DiscoverInfo.Identity defaultIdentity = new DiscoverInfo.Identity("client", "Smack", "pc");
  private Set<DiscoverInfo.Identity> identities = new HashSet();
  private DiscoverInfo.Identity identity = defaultIdentity;
  private EntityCapsManager capsManager;
  private static Map<Connection, ServiceDiscoveryManager> instances = Collections.synchronizedMap(new WeakHashMap());
  private WeakReference<Connection> connection;
  private final Set<String> features = new HashSet();
  private DataForm extendedInfo = null;
  private Map<String, NodeInformationProvider> nodeInformationProviders = new ConcurrentHashMap();

  static
  {
    Connection.addConnectionCreationListener(new ConnectionCreationListener()
    {
      public void connectionCreated(Connection paramAnonymousConnection)
      {
        ServiceDiscoveryManager.getInstanceFor(paramAnonymousConnection);
      }
    });
  }

  public static void setDefaultIdentity(DiscoverInfo.Identity paramIdentity)
  {
    defaultIdentity = paramIdentity;
  }

  @Deprecated
  public ServiceDiscoveryManager(Connection paramConnection)
  {
    this.connection = new WeakReference(paramConnection);
    instances.put(paramConnection, this);
    addFeature("http://jabber.org/protocol/disco#info");
    addFeature("http://jabber.org/protocol/disco#items");
    PacketTypeFilter localPacketTypeFilter = new PacketTypeFilter(DiscoverItems.class);
    Object localObject = new PacketListener()
    {
      public void processPacket(Packet paramAnonymousPacket)
      {
        Connection localConnection = (Connection)ServiceDiscoveryManager.this.connection.get();
        if (localConnection == null)
          return;
        DiscoverItems localDiscoverItems1 = (DiscoverItems)paramAnonymousPacket;
        if ((localDiscoverItems1 != null) && (localDiscoverItems1.getType() == IQ.Type.GET))
        {
          DiscoverItems localDiscoverItems2 = new DiscoverItems();
          localDiscoverItems2.setType(IQ.Type.RESULT);
          localDiscoverItems2.setTo(localDiscoverItems1.getFrom());
          localDiscoverItems2.setPacketID(localDiscoverItems1.getPacketID());
          localDiscoverItems2.setNode(localDiscoverItems1.getNode());
          NodeInformationProvider localNodeInformationProvider = ServiceDiscoveryManager.this.getNodeInformationProvider(localDiscoverItems1.getNode());
          if (localNodeInformationProvider != null)
          {
            localDiscoverItems2.addItems(localNodeInformationProvider.getNodeItems());
            localDiscoverItems2.addExtensions(localNodeInformationProvider.getNodePacketExtensions());
          }
          else if (localDiscoverItems1.getNode() != null)
          {
            localDiscoverItems2.setType(IQ.Type.ERROR);
            localDiscoverItems2.setError(new XMPPError(XMPPError.Condition.item_not_found));
          }
          localConnection.sendPacket(localDiscoverItems2);
        }
      }
    };
    paramConnection.addPacketListener((PacketListener)localObject, localPacketTypeFilter);
    localPacketTypeFilter = new PacketTypeFilter(DiscoverInfo.class);
    localObject = new PacketListener()
    {
      public void processPacket(Packet paramAnonymousPacket)
      {
        Connection localConnection = (Connection)ServiceDiscoveryManager.this.connection.get();
        if (localConnection == null)
          return;
        DiscoverInfo localDiscoverInfo1 = (DiscoverInfo)paramAnonymousPacket;
        if ((localDiscoverInfo1 != null) && (localDiscoverInfo1.getType() == IQ.Type.GET))
        {
          DiscoverInfo localDiscoverInfo2 = new DiscoverInfo();
          localDiscoverInfo2.setType(IQ.Type.RESULT);
          localDiscoverInfo2.setTo(localDiscoverInfo1.getFrom());
          localDiscoverInfo2.setPacketID(localDiscoverInfo1.getPacketID());
          localDiscoverInfo2.setNode(localDiscoverInfo1.getNode());
          if (localDiscoverInfo1.getNode() == null)
          {
            ServiceDiscoveryManager.this.addDiscoverInfoTo(localDiscoverInfo2);
          }
          else
          {
            NodeInformationProvider localNodeInformationProvider = ServiceDiscoveryManager.this.getNodeInformationProvider(localDiscoverInfo1.getNode());
            if (localNodeInformationProvider != null)
            {
              localDiscoverInfo2.addFeatures(localNodeInformationProvider.getNodeFeatures());
              localDiscoverInfo2.addIdentities(localNodeInformationProvider.getNodeIdentities());
              localDiscoverInfo2.addExtensions(localNodeInformationProvider.getNodePacketExtensions());
            }
            else
            {
              localDiscoverInfo2.setType(IQ.Type.ERROR);
              localDiscoverInfo2.setError(new XMPPError(XMPPError.Condition.item_not_found));
            }
          }
          localConnection.sendPacket(localDiscoverInfo2);
        }
      }
    };
    paramConnection.addPacketListener((PacketListener)localObject, localPacketTypeFilter);
  }

  public String getIdentityName()
  {
    return this.identity.getName();
  }

  public void setIdentityName(String paramString)
  {
    this.identity.setName(paramString);
    renewEntityCapsVersion();
  }

  public String getIdentityType()
  {
    return this.identity.getType();
  }

  public void setIdentityType(String paramString)
  {
    this.identity.setType(paramString);
    renewEntityCapsVersion();
  }

  public void addIdentity(DiscoverInfo.Identity paramIdentity)
  {
    this.identities.add(paramIdentity);
    renewEntityCapsVersion();
  }

  public boolean removeIdentity(DiscoverInfo.Identity paramIdentity)
  {
    if (paramIdentity.equals(this.identity))
      return false;
    this.identities.remove(paramIdentity);
    renewEntityCapsVersion();
    return true;
  }

  public Set<DiscoverInfo.Identity> getIdentities()
  {
    HashSet localHashSet = new HashSet(this.identities);
    localHashSet.add(defaultIdentity);
    return Collections.unmodifiableSet(localHashSet);
  }

  public static synchronized ServiceDiscoveryManager getInstanceFor(Connection paramConnection)
  {
    ServiceDiscoveryManager localServiceDiscoveryManager = (ServiceDiscoveryManager)instances.get(paramConnection);
    if (localServiceDiscoveryManager == null)
      localServiceDiscoveryManager = new ServiceDiscoveryManager(paramConnection);
    return localServiceDiscoveryManager;
  }

  public void addDiscoverInfoTo(DiscoverInfo paramDiscoverInfo)
  {
    paramDiscoverInfo.addIdentities(getIdentities());
    synchronized (this.features)
    {
      Iterator localIterator = getFeatures();
      while (localIterator.hasNext())
        paramDiscoverInfo.addFeature((String)localIterator.next());
      paramDiscoverInfo.addExtension(this.extendedInfo);
    }
  }

  private NodeInformationProvider getNodeInformationProvider(String paramString)
  {
    if (paramString == null)
      return null;
    return (NodeInformationProvider)this.nodeInformationProviders.get(paramString);
  }

  public void setNodeInformationProvider(String paramString, NodeInformationProvider paramNodeInformationProvider)
  {
    this.nodeInformationProviders.put(paramString, paramNodeInformationProvider);
  }

  public void removeNodeInformationProvider(String paramString)
  {
    this.nodeInformationProviders.remove(paramString);
  }

  public Iterator<String> getFeatures()
  {
    synchronized (this.features)
    {
      return Collections.unmodifiableList(new ArrayList(this.features)).iterator();
    }
  }

  public List<String> getFeaturesList()
  {
    synchronized (this.features)
    {
      return new LinkedList(this.features);
    }
  }

  public void addFeature(String paramString)
  {
    synchronized (this.features)
    {
      this.features.add(paramString);
      renewEntityCapsVersion();
    }
  }

  public void removeFeature(String paramString)
  {
    synchronized (this.features)
    {
      this.features.remove(paramString);
      renewEntityCapsVersion();
    }
  }

  public boolean includesFeature(String paramString)
  {
    synchronized (this.features)
    {
      return this.features.contains(paramString);
    }
  }

  public void setExtendedInfo(DataForm paramDataForm)
  {
    this.extendedInfo = paramDataForm;
    renewEntityCapsVersion();
  }

  public DataForm getExtendedInfo()
  {
    return this.extendedInfo;
  }

  public List<PacketExtension> getExtendedInfoAsList()
  {
    ArrayList localArrayList = null;
    if (this.extendedInfo != null)
    {
      localArrayList = new ArrayList(1);
      localArrayList.add(this.extendedInfo);
    }
    return localArrayList;
  }

  public void removeExtendedInfo()
  {
    this.extendedInfo = null;
    renewEntityCapsVersion();
  }

  public DiscoverInfo discoverInfo(String paramString)
    throws XMPPException
  {
    if (paramString == null)
      return discoverInfo(null, null);
    DiscoverInfo localDiscoverInfo = EntityCapsManager.getDiscoverInfoByUser(paramString);
    if (localDiscoverInfo != null)
      return localDiscoverInfo;
    EntityCapsManager.NodeVerHash localNodeVerHash = EntityCapsManager.getNodeVerHashByJid(paramString);
    localDiscoverInfo = discoverInfo(paramString, localNodeVerHash != null ? localNodeVerHash.getNodeVer() : null);
    if ((localNodeVerHash != null) && (EntityCapsManager.verifyDiscoverInfoVersion(localNodeVerHash.getVer(), localNodeVerHash.getHash(), localDiscoverInfo)))
      EntityCapsManager.addDiscoverInfoByNode(localNodeVerHash.getNodeVer(), localDiscoverInfo);
    return localDiscoverInfo;
  }

  public DiscoverInfo discoverInfo(String paramString1, String paramString2)
    throws XMPPException
  {
    Connection localConnection = (Connection)this.connection.get();
    if (localConnection == null)
      throw new XMPPException("Connection instance already gc'ed");
    DiscoverInfo localDiscoverInfo = new DiscoverInfo();
    localDiscoverInfo.setType(IQ.Type.GET);
    localDiscoverInfo.setTo(paramString1);
    localDiscoverInfo.setNode(paramString2);
    PacketCollector localPacketCollector = localConnection.createPacketCollector(new PacketIDFilter(localDiscoverInfo.getPacketID()));
    localConnection.sendPacket(localDiscoverInfo);
    IQ localIQ = (IQ)localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    localPacketCollector.cancel();
    if (localIQ == null)
      throw new XMPPException("No response from the server.");
    if (localIQ.getType() == IQ.Type.ERROR)
      throw new XMPPException(localIQ.getError());
    return (DiscoverInfo)localIQ;
  }

  public DiscoverItems discoverItems(String paramString)
    throws XMPPException
  {
    return discoverItems(paramString, null);
  }

  public DiscoverItems discoverItems(String paramString1, String paramString2)
    throws XMPPException
  {
    Connection localConnection = (Connection)this.connection.get();
    if (localConnection == null)
      throw new XMPPException("Connection instance already gc'ed");
    DiscoverItems localDiscoverItems = new DiscoverItems();
    localDiscoverItems.setType(IQ.Type.GET);
    localDiscoverItems.setTo(paramString1);
    localDiscoverItems.setNode(paramString2);
    PacketCollector localPacketCollector = localConnection.createPacketCollector(new PacketIDFilter(localDiscoverItems.getPacketID()));
    localConnection.sendPacket(localDiscoverItems);
    IQ localIQ = (IQ)localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    localPacketCollector.cancel();
    if (localIQ == null)
      throw new XMPPException("No response from the server.");
    if (localIQ.getType() == IQ.Type.ERROR)
      throw new XMPPException(localIQ.getError());
    return (DiscoverItems)localIQ;
  }

  public boolean canPublishItems(String paramString)
    throws XMPPException
  {
    DiscoverInfo localDiscoverInfo = discoverInfo(paramString);
    return canPublishItems(localDiscoverInfo);
  }

  public static boolean canPublishItems(DiscoverInfo paramDiscoverInfo)
  {
    return paramDiscoverInfo.containsFeature("http://jabber.org/protocol/disco#publish");
  }

  public void publishItems(String paramString, DiscoverItems paramDiscoverItems)
    throws XMPPException
  {
    publishItems(paramString, null, paramDiscoverItems);
  }

  public void publishItems(String paramString1, String paramString2, DiscoverItems paramDiscoverItems)
    throws XMPPException
  {
    Connection localConnection = (Connection)this.connection.get();
    if (localConnection == null)
      throw new XMPPException("Connection instance already gc'ed");
    paramDiscoverItems.setType(IQ.Type.SET);
    paramDiscoverItems.setTo(paramString1);
    paramDiscoverItems.setNode(paramString2);
    PacketCollector localPacketCollector = localConnection.createPacketCollector(new PacketIDFilter(paramDiscoverItems.getPacketID()));
    localConnection.sendPacket(paramDiscoverItems);
    IQ localIQ = (IQ)localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    localPacketCollector.cancel();
    if (localIQ == null)
      throw new XMPPException("No response from the server.");
    if (localIQ.getType() == IQ.Type.ERROR)
      throw new XMPPException(localIQ.getError());
  }

  public void setEntityCapsManager(EntityCapsManager paramEntityCapsManager)
  {
    this.capsManager = paramEntityCapsManager;
  }

  private void renewEntityCapsVersion()
  {
    if ((this.capsManager != null) && (this.capsManager.entityCapsEnabled()))
      this.capsManager.updateLocalEntityCaps();
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     org.jivesoftware.smackx.ServiceDiscoveryManager
 * JD-Core Version:    0.6.1
 */